// components/scrollViews/scrollViews.js
Component({

  /**
   * 组件的属性列表
   */
  properties: {
    Class: {
      type: Array,
      value: []
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    curIndex: 0,
    curId: "",
    heightArray: [0],
    scrollTop: 0
  },
  /**
   组件的方法列表
   **/
  methods: {
    
    goRight(e) {
      // 1.获取index
      // 2.把index复制给curIndex
      // 3.判断index==curIndex,相等添加active样式,否则为空
      console.log(e);
      let {
        index
      } = e.currentTarget.dataset
      let {
        id
      } = e.currentTarget.dataset
      this.setData({
        curIndex: index,
        curId: id
      })
    },
    // 获取右侧高度
    getHeight() {
      let _this = this
      this.data.Class.forEach(item => {
        const query = this.createSelectorQuery()
        // console.log(query);
        query.select('#id-' + item.typeName.id).boundingClientRect()
        query.selectViewport().scrollOffset()
        query.exec(function (res) {
          // console.log(res);
          let height = Math.floor(res[0].top + res[0].height)
          // console.log(height);
          _this.data.heightArray.push(height)
        })
      });
      console.log(_this.data.heightArray);
    },
    // 右侧滚动
    scroll(e) {
      // console.log(e);
      this.setData({
        scrollTop: Math.ceil(e.detail.scrollTop)
      })
      // 判断滚动高度是否在数组的i,i+1的范围内,如果在返回
      // 复制curIndex:i
      for (let i = 0; i < this.data.heightArray.length; i++) {
        let top0 = this.data.heightArray[i]
        // console.log(top0, this.data.heightArray);
        let top1 = this.data.heightArray[i + 1]
        let scrollTop = this.data.scrollTop
        if (scrollTop >= top0 && scrollTop < top1) {
          this.setData({
            curIndex: i
          })
          // console.log(this.data.curIndex, i);
        }
      }
    }
  },
  lifetimes: {
    attached() {
      setTimeout(() => {
        this.getHeight()
      }, 1000)
    }
  }
})